package demo8;

import java.util.Stack;

class MyQueue {
    public Stack<Integer> stack1;
    public Stack<Integer> stack2;
    public MyQueue() {
        stack1 = new Stack<>();
        stack2 = new Stack<>();
    }
    
    //放的时候都放在stack1中
    public void push(int x) {
        stack1.push(x);
    }
    
    //出的时候都在stack2出
    public int pop() {
    if (isEmpty()) {
            return -1;
        }
        if (!stack2.empty()) {
            return stack2.pop();
        } else {
            while (!stack1.empty()) {
                stack2.push(stack1.pop());
            }
            return stack2.pop();
        }
    }
    
    public int peek() {
        if (isEmpty()) {
            return -1;
        }
        if (!stack2.empty()) {
            return stack2.peek();
        } else {
            while (!stack1.empty()) {
                int n = stack1.pop();
                stack2.push(n);
            }
            return stack2.peek();
        }
    }
    
    //判断队列是不是为空
    public boolean isEmpty() {
        return stack1.empty() && stack2.empty();
    }
}

/**
 * Your MyQueue object will be instantiated and called as such:
 * MyQueue obj = new MyQueue();
 * obj.push(x);
 * int param_2 = obj.pop();
 * int param_3 = obj.peek();
 * boolean param_4 = obj.empty();
 */